package com.hp.gxw.api;

import com.hp.gxw.config.DataBaseConfig;
import com.hp.gxw.config.MesApiConfig;
import com.hp.gxw.config.MesUrlConfig;
import com.hp.gxw.utils.HttpRequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/trigger")
@Slf4j
public class ReceiptTriggerApi {

    @Autowired
    private HttpRequestUtils httpRequestUtils;

    @Autowired
    private MesApiConfig mesApiConfig;

    @RequestMapping("/afterInsert/{type}/{param}")
    public  void afterInsert(@PathVariable("type") String type, @PathVariable("param") String param){
        System.out.println("type:"+type);
        System.out.println("param:"+param);
        Map<String, Object> transferParam = null;
        Statement inventoryStatement = null;
        ResultSet inventoryResultSet = null;
        List<MesUrlConfig> urllist = mesApiConfig.getUrllist();
        switch (type){
            case "Inventory":
                try {
                    String sql = "select cInvCode,cInvName " +
                            "from Inventory " +
                            "where cInvCode='"+param+"'";
                    inventoryStatement = DataBaseConfig.connection.createStatement();
                    inventoryResultSet = inventoryStatement.executeQuery(sql);
                    while (inventoryResultSet.next()){
                        transferParam = new HashMap<>();
                        transferParam.put("cInvCode", inventoryResultSet.getObject("cInvCode").toString());
                        transferParam.put("cInvName", inventoryResultSet.getObject("cInvName").toString());
                    }
                }catch (SQLException e){
                    e.printStackTrace();
                    log.error("查询账套数据异常");
                }finally {
                    try {
                        if(inventoryStatement != null){
                            inventoryStatement.close();
                        }
                        if(inventoryResultSet != null){
                            inventoryResultSet.close();
                        }
                    }catch (SQLException e){
                        e.printStackTrace();
                    }

                }
                if(transferParam != null){
                    //传递到MES物料档案接口
                    for (MesUrlConfig mesUrlConfig : urllist) {
                        if(mesUrlConfig.getType().equals(type)){
                            httpRequestUtils.request(mesUrlConfig.getUrl(), transferParam, type, "POST");
                            break;
                        }
                    }
                }
                break;
        }
    }
}
